{% extends 'template.html' %}
{% load staticfiles %}

{% block username %}
    {{ request.user.username }}
{% endblock %}

{% block content %}
    <div id="page-wrapper">
        <div class="row">
            <div class="col-lg-12">
                <h1 class="page-header">sls 文件管理</h1>
                {% if current_page %}
                    <input id="specific_page" style="display: none;" value="{{ current_page }}">
                {% else %}
                    <input id="specific_page" style="display: none;" value="0">
                {% endif %}
            </div>
        </div>
        <div class="panel panel-info">
            <div class="panel-heading">
                <h5 class="panel-title">
                    <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne"><strong>点击查看详细说明</strong></a>
                </h5>
            </div>
            <div id="collapseOne" class="panel-collapse collapse">
                <div class="panel-body">
                    <h5><strong>执行任务流程</strong></h5>
                    <p><i class="fa fa-check"></i> 新建 sls 文件，填写任务名/配置文件名/配置内容/远程推送路径</p>
                    <p><i class="fa fa-check"></i> 点击“未推送”按钮，推送 sls 文件，观察按钮变化。如果需要重新推送，请刷新页面，或点击依次“修改”-“保存”即可</p>
                    <p><i class="fa fa-check"></i> 【若是pillar文件，请跳过这一步】选择执行主机 --> 选择筛选条件 --> 勾选主机 --> 准备执行 --> 开始执行 -->
                        观察控制台输出结果</p>
                    <p><i class="fa fa-check"></i> 【若是可执行sls文件，请忽略这一步】点击“刷新pillar配置”，将pillar信息同步到minion，观察按钮变化</p>
                    <h5><strong>修改 / 回滚配置流程</strong></h5>
                    <p><i class="fa fa-check"></i> 修改-修改配置内容-保存</p>
                    <p><i class="fa fa-check"></i> 历史记录-查看详情-回滚</p>
                    <h5><strong>注意事项</strong></h5>
                    <p><i class="fa fa-check"></i> 如果任务状态为禁用，则不允许推送配置文件</p>
                    <p><i class="fa fa-check"></i> 如果任务推送状态为未推送，则不允许执行任务</p>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-lg-12">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <div class="btn-group">
                            <a>
                                <button type="button" class="btn btn-success" onclick="add_salt_task()">新增 sls 文件
                                </button>
                            </a>
                            <a>
                                <button id='bt-del' data-text="确定删除saltstack任务? 0个" type="button"
                                        class="btn btn-danger">删除
                                </button>
                            </a>
                        </div>
                        <div class="btn-group pull-right">
                            <button class="btn btn-info" type="button" id="sync_pillar" data-text="确定刷新pillar配置?">
                                刷新pillar配置
                            </button>
                        </div>
                    </div>
                    <div class="panel-body">
                        <form>
                            <div class="form-group">
                                {% if request.user.is_superuser %}
                                    <label class="checkbox-inline">
                                        <input type="checkbox" value="7" class="toggle-visiable">修改人
                                    </label>
                                    <label class="checkbox-inline">
                                        <input type="checkbox" value="13" class="toggle-visiable">执行人
                                    </label>
                                    <label class="checkbox-inline">
                                        <input type="checkbox" value="10" class="toggle-visiable">推送人
                                    </label>
                                    <label class="checkbox-inline">
                                        <input type="checkbox" value="11" class="toggle-visiable">推送时间
                                    </label>
                                {% endif %}
                            </div>
                        </form>
                        {% if salt_tasks %}
                            <table id="mytable" width="100%" cellspacing="0"
                                   class="table table-striped table-hover">
                                <thead>
                                <tr>
                                    <th class="center sorting_disabled">
                                        <label class="pos-rel">
                                            <input id='chb-all' type="checkbox"/>
                                        </label>
                                    </th>
                                    <th style="display: none;">任务id</th>
                                    <th>任务名称</th>
                                    <th>任务状态</th>
                                    <th>配置文件名</th>
                                    <th>远程路径</th>
                                    <th>修改</th>
                                    <th>修改人</th>
                                    <th>修改时间</th>
                                    <th>推送</th>
                                    <th>推送人</th>
                                    <th>推送时间</th>
                                    <th>执行</th>
                                    <th>执行人</th>
                                    <th>执行时间</th>
                                    <th>执行结果</th>
                                </tr>
                                </thead>
                                <tbody>
                                {% for task in salt_tasks %}
                                    <tr>
                                        <td class="dt-body-center"><input type="checkbox"></td>
                                        <td style="display: none;" id="{{ task.salt_task.id }}"
                                            value="{{ task.salt_task.id }}"
                                            name="{{ task.salt_task.id }}">{{ task.salt_task.id }}</td>
                                        <td>
                                            {% if 'pillar' in task.push_path %}<label class="label label-warning">
                                                pillar{% else %}<label class="label label-info">
                                                salt{% endif %}</label> {{ task.salt_task.name }}
                                        </td>
                                        <td {% if task.salt_task.status == 1 %}class="text-success"
                                            {% else %}class="text-danger"{% endif %}>
                                            {{ task.salt_task.get_status_display }}
                                        </td>
                                        <td>{{ task.filename }}</td>
                                        <td>{{ task.push_path }}</td>
                                        <td>
                                            <button class="btn btn-sm btn-info" onclick="edit({{ task.id }})">修改
                                            </button>
                                            <a href="/assets/salt_config_history/{{ task.id }}"
                                               class="btn btn-sm btn-danger">历史记录</a>
                                        </td>
                                        <td>{{ task.modified_user.username }}</td>
                                        <td>{{ task.modified_time|date:"Y-m-d H:i:s" }}</td>
                                        {% if task.release_set.all %}
                                            {% for release in task.release_set.all %}
                                                <td>
                                                    <!--1:已推送， 2：未推送 -->
                                                    {% if release.status == 1 %}
                                                        <button class="btn btn-sm btn-success"
                                                                disabled="true">{{ release.get_status_display }}</button>
                                                    {% else %}
                                                        <!-- 如果任务状态为禁用，则该按钮禁用 -->
                                                        <button class="btn btn-sm btn-warning"
                                                                {% if task.salt_task.status == 0 %}disabled="true"{% endif %}
                                                                onclick="push('{{ task.id }}', '{{ task.filename }}')"
                                                                id="bt-push-{{ task.id }}">{{ release.get_status_display }}</button>
                                                    {% endif %}
                                                </td>
                                                <td>{{ release.release_user.username }}</td>
                                                <td>{{ release.release_time|date:"Y-m-d H:i:s" }}</td>
                                            {% endfor %}
                                        {% else %}
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                        {% endif %}
                                        <td>
                                            <a class="btn btn-sm btn-primary" id="ready_execute-{{ task.id }}"
                                               {% if task.salt_task.status == 0 or task.if_exist_unreleased_status %}disabled="true"{% endif %}
                                               href="{% url 'system_cron_execute' task.salt_task.id %}">选择执行主机</a>
                                            <a href="/assets/salt_task_execute_history/{{ task.salt_task_id }}/"
                                               class="btn btn-sm btn-danger">历史记录</a>
                                        </td>
                                        <td>{{ task.get_last_execute_user }}</td>
                                        <td>{{ task.get_last_execute_time|date:"Y-m-d H:i:s" }}</td>
                                        <td>
                                            {% if task.get_last_execute_result == '全部失败' %}
                                                <button class="btn btn-sm btn-danger"
                                                        disabled>{{ task.get_last_execute_result }}</button>
                                            {% elif task.get_last_execute_result == '部分失败' %}
                                                <button class="btn btn-sm btn-warning"
                                                        disabled>{{ task.get_last_execute_result }}</button>
                                            {% elif task.get_last_execute_result == '全部成功' %}
                                                <button class="btn btn-sm btn-success"
                                                        disabled>{{ task.get_last_execute_result }}</button>
                                            {% else %}
                                            {% endif %}
                                        </td>
                                    </tr>
                                {% endfor %}
                                </tbody>
                            </table>
                        {% else %}
                            <p>还没有任务数据，请先添加 SaltStack 任务！</p>
                        {% endif %}
                    </div>
                </div>
            </div>

            <!-- Edit Modal-->
            <div class="modal fade" id="Modal-edit" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                <div class="modal-dialog" style="width:1000px;">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                            <h4 class="modal-title" id="myModalLabel">配置修改</h4>
                        </div>
                        <form method="POST" action="/assets/save_salt_config/">
                            <div class="modal-body" style="height:750px;overflow:auto;">
                                <input type="text" style="display: none" id="config_id" name="config_id" value="">
                                <input id="current_page" style="display: none" name="current_page">
                                <div class="form-group row">
                                    <label class="control-label col-md-2">任务名称</label>
                                    <div class="col-md-5">
                                        <input name="task_name" id="task_name" value="" class="form-control">
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label class="control-label col-md-2">任务状态</label>
                                    <div class="form-check col-md-2">
                                        <label class="form-check-label">
                                            <input name="status" id="status_enable" value="1"
                                                   class="form-check-input" type="radio">启用
                                        </label>
                                    </div>
                                    <div class="form-check col-md-3">
                                        <label class="form-check-label">
                                            <input name="status" id="status_disable" value="0"
                                                   class="form-check-input" type="radio">禁用
                                        </label>
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label class="control-label col-md-2">配置文件名</label>
                                    <div class="col-md-5">
                                        <input name="filename" id="filename" value="" class="form-control">
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label class="control-label col-md-2">推送远程路径</label>
                                    <div class="col-md-5">
                                        <input name="push_path" id="push_path" value="" class="form-control"
                                               placeholder="/srv/salt/">
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label class="control-label col-md-2">修改原因备注</label>
                                    <div class="col-md-9">
                                        <textarea class="form-control" rows="3" cols="100" id="remark" name="remark"
                                                  value="" placeholder="请填写修改原因（可选）！"></textarea>
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label class="control-label col-md-2">配置内容</label>
                                    <div class="col-md-9">
                                        <textarea rows="30" cols="100" id="content" name="content" value=""></textarea>
                                    </div>
                                </div>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                                <button type="submit" class="btn btn-primary" name='action'>保存</button>
                            </div>
                        </form>
                    </div><!-- /.modal-content -->
                </div><!-- /.modal-dialog -->
            </div>

            <!-- Push Confirm Modal-->
            <div class="modal fade" id="Modal-push" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                            <p class="modal-title" id="myModalLabel">是否确认推送配置文件： <span id="push_filename"></span></p>
                        </div>
                        <form>
                            <input style="display: none;" id="push_filename_id" name="push_filename_id" value="">
                            <div class="modal-footer">
                                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                                <button type="button" class="btn btn-primary" id="push_confirm" name='action'>确认
                                </button>
                            </div>
                        </form>
                    </div><!-- /.modal-content -->
                </div><!-- /.modal-dialog -->
            </div>

            <!-- Push Result Notice Modal-->
            <div class="modal fade" id="Modal-result" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                            <p class="modal-title" id="myModalLabel"><span id="push-result"></span></p>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-primary" data-dismiss="modal">关闭</button>
                        </div>
                    </div><!-- /.modal-content -->
                </div><!-- /.modal-dialog -->
            </div>

            <!-- Add Task Modal-->
            <div class="modal fade" id="Modal-Add" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                <div class="modal-dialog" style="width:1000px;">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                            <h4 class="modal-title" id="myModalLabel">新增 SaltStack 任务</h4>
                        </div>
                        <form method="POST" action="/assets/add_salt_task/">
                            <div class="modal-body" style="height:750px;overflow:auto;">
                                <div class="form-group row">
                                    <label class="control-label col-md-2">任务名称</label>
                                    <div class="col-md-8">
                                        <input name="add_task_name" value="" class="form-control" placeholder="请输入任务名称">
                                        <input id="current_page2" style="display: none" name="current_page">
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label class="control-label col-md-2">任务状态</label>
                                    <div class="form-check col-md-2">
                                        <label class="form-check-label">
                                            <input name="add_status" value="1"
                                                   class="form-check-input" checked="true" type="radio">启用
                                        </label>
                                    </div>
                                    <div class="form-check col-md-3">
                                        <label class="form-check-label">
                                            <input name="add_status" value="0"
                                                   class="form-check-input" type="radio">禁用
                                        </label>
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label class="control-label col-md-2">配置文件名</label>
                                    <div class="col-md-5">
                                        <input name="add_filename" value="" class="form-control" placeholder="请输入配置文件名">
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label class="control-label col-md-2">远程推送路径</label>
                                    <div class="col-md-5">
                                        <input name="add_push_path" value="" class="form-control"
                                               placeholder="/srv/salt/">
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label class="control-label col-md-2">配置内容</label>
                                    <div class="col-md-9">
                                      <textarea rows="30" cols="100" name="add_content"
                                                value="" placeholder="请输入配置内容"></textarea>
                                    </div>
                                </div>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                                <button type="submit" class="btn btn-primary" name='action'>新增</button>
                            </div>
                        </form>
                    </div><!-- /.modal-content -->
                </div><!-- /.modal-dialog -->
            </div>

            <div id="is_superuser" data-is-superuser="{{ is_superuser }}"></div>

        </div>
    </div>
{% endblock %}
{% block bodyjs %}
    <script src="/static/js/reconnecting-websocket.js"></script>
    <script src="/static/js/jquery.dataTables.js"></script>
    <script src="/static/js/system_cron_list.js?v=20190725001"></script>
    <script src="/static/js/dataTables.colVis.js"></script>
{% endblock %}
{% block css %}
    <link href="/static/css/jquery.dataTables.css" rel="stylesheet" type="text/css">
    <link href="/static/css/dataTables.colVis.css" rel="stylesheet" type="text/css">
    <link href="/static/css/system_cron_list.css" rel="stylesheet" type="text/css">
{% endblock %}
